package com.sina.shop.service.goods;

import com.sina.shop.core.exception.client.DataException;
import com.sina.shop.core.exception.server.CreateException;
import com.sina.shop.core.exception.server.ResourceExistsException;
import com.sina.shop.core.exception.server.UpdateException;
import com.sina.shop.dao.goods.GoodsCommentDao;
import com.sina.shop.domain.goods.Goods;
import com.sina.shop.domain.goods.GoodsComment;
import com.sina.shop.service.ShopDataSourceService;
import com.sina.shop.util.Assert;
import com.sina.shop.util.ResultCode;
import com.sina.shop.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.sina.shop.service.ServiceConstant.SHOP_TX;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-02-07-9:36
 */
@Service("goodsCommentService")
public class GoodsCommentServiceImpl extends ShopDataSourceService implements GoodsCommentService {

    @Autowired
    @Qualifier("goodsCommentDao")
    private GoodsCommentDao goodsCommentDao;

    @Override
    @Transactional(value = SHOP_TX, readOnly = true, isolation = Isolation.READ_COMMITTED)
    public List<GoodsComment> getGoodsCommentList(GoodsComment goodsComment, Long[] count) {

        if (count != null && count.length > 0) {
            count[0] = goodsCommentDao.getGoodsCommentCount(goodsComment);
        }

        return goodsCommentDao.getGoodsCommentList(goodsComment);
    }

    @Override
    @Transactional(value = SHOP_TX, readOnly = true, isolation = Isolation.READ_COMMITTED)
    public GoodsComment getGoodsComment(Long id) {
        return goodsCommentDao.get(GoodsComment.class, id);
    }

    @Override
    @Transactional(value = SHOP_TX, isolation = Isolation.REPEATABLE_READ)
    public Integer deleteGoodsComment(Long id) throws DataException, UpdateException {
        GoodsComment goodsComment = goodsCommentDao.get(GoodsComment.class, id);
        Assert.assertNotNull(goodsComment, "GoodsComment[id=%s] not exists", goodsComment.getId());

        try {
            return goodsCommentDao.deleteGoodsComment(id);
        } catch (DataAccessException e) {
            LOG.error(" {} delete GoodsComment error.", GoodsComment.class, e);
            throw new UpdateException(ResultCode.dataAccessError, e.getMessage(), e);
        }
    }

    @Override
    @Transactional(value = SHOP_TX, isolation = Isolation.READ_COMMITTED)
    public GoodsComment saveGoodsComment(GoodsComment goodsComment) throws CreateException, ResourceExistsException, DataException {

        try {
            goodsCommentDao.save(goodsComment);
            goodsCommentDao.flush();
            return goodsComment;
        } catch (DataAccessException e) {
            LOG.error("{} save GoodsComment error.", Goods.class, e);
            throw new UpdateException(ResultCode.dataAccessError, e.getMessage(), e);
        }
    }

    @Override
    @Transactional(value = SHOP_TX, isolation = Isolation.REPEATABLE_READ)
    public GoodsComment updateGoodsComment(GoodsComment goodsComment) throws UpdateException, DataException {
        GoodsComment g = goodsCommentDao.get(GoodsComment.class, goodsComment.getId());
        Assert.assertNotNull(g, "GoodsComment[id=%s] not exists", goodsComment.getId());

        try {
            g = updateGoodsCommentFiled(goodsComment, g);
            goodsCommentDao.flush();

            return g;
        } catch (DataAccessException e) {
            LOG.error("{} update GoodsComment error.", Goods.class, e);
            throw new UpdateException(ResultCode.dataAccessError, e.getMessage(), e);
        }
    }

    private GoodsComment updateGoodsCommentFiled(GoodsComment oldGoodsComment, GoodsComment newGoodsComment) {


        if (!StringUtils.isEmpty(oldGoodsComment.getType())) {
            newGoodsComment.setType(oldGoodsComment.getType());
        }
        if (oldGoodsComment.getBit() != null) {
            newGoodsComment.setBit(oldGoodsComment.getBit());
        }
        if (!StringUtils.isEmpty(oldGoodsComment.getContents())) {
            newGoodsComment.setContents(oldGoodsComment.getContents());
        }

        return newGoodsComment;
    }
}
